package com.rgs.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.rgs.ssm.domain.Member;
import com.rgs.ssm.domain.Orders;
import com.rgs.ssm.domain.Product;
import com.rgs.ssm.service.IMemberService;
import com.rgs.ssm.service.IOrderService;
import com.rgs.ssm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @create 2021-02-17 14:19
 * 订单控制器
 **/
@Controller
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IMemberService memberService;


    /**
     * 查询全部订单 实现分页
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "size",required = true,defaultValue = "5")Integer size){
        ModelAndView mv = new ModelAndView();
        List<Orders> ps = orderService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(ps);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("orders-list");
        return mv;
    }

    /**
     * 去添加订单
     */
    @RequestMapping("/toSave.do")
    public ModelAndView toSave(){
        ModelAndView mv = new ModelAndView();

        List<Product> allProduct = productService.findAll();
        mv.addObject("allProduct",allProduct);

        List<Member> allMember = memberService.findAll();
        mv.addObject("allMember",allMember);

        mv.setViewName("orders-add");
        return mv;
    }

    /**
     * 添加订单
     * @param orders
     */
    @RequestMapping("/save.do")
    public String saveOrders(Orders orders){
        System.out.println(orders);

        //下单时间为保存的时间
        orders.setOrderTime(new Date());
        orderService.saveOrders(orders);
        System.out.println("添加订单完毕！");
        return "redirect:/orders/findAll.do";
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/deleteById.do")
    public String  deleteById(@RequestParam(name = "id",required = true)Integer id){
        orderService.delete(id);
        return "redirect:/orders/findAll.do";
    }

    /**
     * 去修改页面
     * @return
     */
    @RequestMapping("/toUpdateById.do")
    public ModelAndView toUpdateById(@RequestParam(name = "id",required = true)Integer id){
        ModelAndView mv = new ModelAndView();
        Orders orders = orderService.findById(id);
        mv.addObject("orders",orders);
        mv.setViewName("orders-update");
        return mv;
    }

    /**
     * 处理修改请求
     * @return
     */
    @RequestMapping("/updateById.do")
    public String updateById(Orders orders){
        orderService.update(orders);
        return "redirect:/orders/findAll.do";
    }

    /**
     * 根据id，查看指定用户详情（包括用户的角色信息，以及角色信息中的权限信息）
     * @param orderId
     * @return
     */
    @RequestMapping("/showDetails.do")
    public ModelAndView showDetails(@RequestParam(name = "id",required = true)Integer orderId){
        System.out.println("收到order/findById.do...");
        System.out.println(orderId.getClass().toString());
        ModelAndView mv = new ModelAndView();
        Orders orders = orderService.findById(orderId);
        mv.addObject("orders",orders);
        mv.setViewName("orders-show");
        return mv;
    }
}
